﻿using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace sydfdl.TMS.BLL
{
    public class BLLT_B_User
    {
        public T_Base<T_B_User> GetAllUserList(string xml)
        {
            T_Base<T_B_User> list = new T_Base<T_B_User>();

            SqlParameter[] para = { 
                                  new SqlParameter("@Para",SqlDbType.Xml,-1)
                                  };
            para[0].Value = xml;

            string cmdText = "P_B_User_GetAllList";

            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, cmdText, para))
            {
                List<T_B_User> data = new List<T_B_User>();
                if (dr.Read() && dr.HasRows)
                    list.total = dr.GetInt32(0);
                if (dr.NextResult())
                {
                    while (dr.Read() && dr.HasRows)
                    {
                        T_B_User item = new T_B_User();
                        item.UID = dr.GetGuid(0);
                        item.UCode = dr.GetString(1);
                        item.UName = dr.GetString(2);
                        item.IsAdmin = dr.GetBoolean(3);
                        item.USex = dr.GetBoolean(4);
                        item.EMail = dr.GetString(5);
                        item.UPhone = dr.GetString(6);
                        item.WeChat = dr.GetString(7);
                        item.QQ = dr.GetString(8);
                        item.Pwd = dr.GetString(9);
                        item.Address = dr.GetString(10);
                        item.IsValid = dr.GetBoolean(11);
                        if (!dr.IsDBNull(12))
                        {
                            item.CID = dr.GetGuid(12);
                        }
                        if (!dr.IsDBNull(13))
                        {
                            item.CName = dr.GetString(13);
                        }
                        item.IsInner = dr.GetBoolean(14);

                        data.Add(item);
                    }

                    list.data = data;
                }
            }

            return list;
        }

        public string SaveUserData(IEnumerable<T_B_User> add, IEnumerable<T_B_User> edit, IEnumerable<T_B_User> remove)
        {
            string addXML = EFHelp<T_B_User>.GetXML(add);
            string editXML = EFHelp<T_B_User>.GetXML(edit);
            string removeXML = EFHelp<T_B_User>.GetXML(remove);

            SqlParameter[] para = { 
                                      new SqlParameter("@AddXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@EditXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@RemoveXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@UserXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                  };
            para[0].Value = addXML;
            para[1].Value = editXML;
            para[2].Value = removeXML;
            para[3].Value = null;
            para[4].Direction = ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, "P_B_User_SaveData", para);

            return (para[4].Value ?? string.Empty).ToString();
        }

        public string SaveUserMenuData(IEnumerable<T_B_UserMenu> edit, string uid)
        {
            string editXML = EFHelp.GetXML(edit);

            SqlParameter[] para = { 
                                      new SqlParameter("@UID",SqlDbType.NVarChar,100),
                                      new SqlParameter("@EditXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@UserXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                   };

            para[0].Value = uid;
            para[1].Value = editXML;
            para[2].Value = null;
            para[3].Direction = ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, "P_B_User_SaveUserMenuData", para);

            return (para[3].Value ?? string.Empty).ToString();
        }

        public List<T_B_UserMenu> GetUserMenu(string uid)
        {
            List<T_B_UserMenu> list = new List<T_B_UserMenu>();

            SqlParameter[] para = { 
                                       new SqlParameter("@UID",SqlDbType.NVarChar,100)
                                   };

            para[0].Value = uid;

            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_User_GetAllMenuByUser", para))
            {
                while (dr.Read() && dr.HasRows)
                {
                    T_B_UserMenu item = new T_B_UserMenu();
                    if (!(dr["MID"] is DBNull))
                    {
                        item.MID = Guid.Parse(dr["MID"].ToString());
                    }
                    if (!(dr["FID"] is DBNull))
                    {
                        item.FID = Guid.Parse(dr["FID"].ToString());
                    }
                    else
                    {
                        item.FID = Guid.Empty;
                    }
                    if (!(dr["MName"] is DBNull))
                    {
                        item.MName = dr["MName"].ToString();
                    }
                    if (!(dr["MUrl"] is DBNull))
                    {
                        item.MUrl = dr["MUrl"].ToString();
                    }
                    if (!(dr["IsMID"] is DBNull))
                    {
                        item.IsMID = Guid.Parse(dr["IsMID"].ToString());
                    }
                    else
                    {
                        item.IsMID = Guid.Empty;
                    }
                    if (!(dr["ButtonXML"] is DBNull))
                    {
                        item.ButtonXML = dr["ButtonXML"].ToString();
                        item.data = new List<T_B_MenuXML>();
                        XElement.Parse(item.ButtonXML).Elements().ToList().ForEach(a =>
                            {
                                T_B_MenuXML _inner = new T_B_MenuXML()
                               {
                                   ID = a.Attribute("ID").Value,
                                   Text = a.Attribute("Text").Value
                               };

                                if (!(dr["InnerXML"] is DBNull))
                                {
                                    item.InnerXML = dr["InnerXML"].ToString();
                                    if (XElement.Parse(item.InnerXML).Elements().Attributes("ID").ToList().Exists(b => b.Value == _inner.ID))
                                    {
                                        _inner.IsChecked = true;
                                    }
                                }
                                item.data.Add(_inner);
                            });
                    }
                    list.Add(item);
                }
            }

            return list;
        }
    }
}
